// pages/defect/adjustment/index.js
import {
  _list,
  _add,
  _update,
  _remove
} from '../../../../api/recipe-Import-method'
import {
  list
} from '../../../../api/ingredients'
import {
  _list as ingredientsParent 
} from '../../../../api/ingredients-parent'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataSource: [],

    disabledSwipe: false,
    addParamsShow: false,
    _metaData: {},
    ingredientsParentList: [],
    columnsIng: []
  },
  toCraft(event) {
    const {
      data,
      item
    } = event.currentTarget.dataset;
    const ancestor = encodeURIComponent(JSON.stringify(this.data._metaData['ancestor'])),
      father = encodeURIComponent(JSON.stringify(item)),
      grandfather = encodeURIComponent(JSON.stringify(this.data._metaData['father']));
 
    wx.navigateTo({
      url: '../tables/index?' + `ancestor=${ancestor}&father=${father}&grandfather=${grandfather}`,
    })
  },
  onSwipeDisabld() {
    // this.setData({
    //   disabledSwipe: true
    // })
  },
  onSwipeEnable() {
    // this.setData({
    //   disabledSwipe: false
    // })
  },
  onEdit(event) {
    const {
      index,
      item
    } = event.currentTarget.dataset;
    const instance = this.selectComponent('#_options');
    instance.open(item, index);
  },
  onAdd() {
    const instance = this.selectComponent('#params');
    instance?.onClear();
    this.onAddParamsShow()
  },
  onGoback(event) {
    const history = event.currentTarget.dataset.history;
 
    wx.navigateBack({
      delta: Number.parseInt(history)
    });
  },
  onDelect(event) {
    const dataSource = this.data.dataSource,
      index = event.currentTarget.dataset.index;
    wx.showModal({
      title: '确定要删除吗？',
      icon: 'none',
      success: res => {
        if (res.confirm) {
          _remove(dataSource[index], () => {
            dataSource.splice(index, 1);
            this.setData({
              dataSource
            })
          })
        }
      }
    })
  },
  fetchData() {
    _list({
      recipeCompositionTypeId: this.data._metaData['father']['id']
    }, res => {
      this.setData({
        dataSource: res.map(it=>{
          return {
            ...it,
            name: this.data.columnsIng.find(item=>{
              return item.id === it.ingredientsId
            })?.name
          }
        })
      })
    })
  },
  fetchIngList() {
    list({
      status: "ANNOUNCED"
    }, res => {
      this.fetchData();
      this.setData({
        columnsIng:  res
      })
    })
  },
  fetchIngredientsParentList(){
    ingredientsParent({},res=>{
       this.setData({
        ingredientsParentList: res
       })
    })
  },
  onUpdateParams() {
    this.fetchData();
  },
  onAddParamsCancel() {
    this.setData({
      addParamsShow: false
    })
  },
  onAddParamsShow() {

    this.setData({
      addParamsShow: true
    })
  },
  onAddParamsSubmit(event) {
    _add({
      recipeCompositionTypeId: this.data._metaData['father']['id'],
      ...event.detail
    }, () => {
      this.fetchData()
    })
    this.onAddParamsCancel();
  },
  fetchParamTypeList(ids) {

  },
  onParamEdit(event) {
    const {
      item,
      data
    } = event.currentTarget.dataset;
    const _optionsInstance = this.selectComponent("#_options");
    _optionsInstance?.open(item, data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    try {
      let _metaData = {
        ancestor: JSON.parse(decodeURIComponent(options['ancestor'])),
        father: JSON.parse(decodeURIComponent(options['father']))
      }
      this.setData({
        _metaData
      })
     this.fetchIngList();
     this.fetchIngredientsParentList();
    } catch (e) {
      wx.navigateBack()
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})